'use strict'
const webpack = require('webpack')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')// webpack5中移除了nodejs核心模块的polyfill自动引入，所以需要手动引入

const productionGzipExtensions = ['js', 'css']
function resolve(dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  publicPath: './',
  runtimeCompiler: true,
  devServer: {
    headers: {
      'Access-Control-Allow-Origin': '*'
    }
  },
  // 解决边缘css告警
  css: {
    extract: process.env.NODE_ENV === 'production' ? {
      ignoreOrder: true
    } : false
  },
  // vue.config.js
  chainWebpack: (config) => {
    config.optimization.minimizer('js')
      .use(new TerserPlugin({
        terserOptions: {
          compress: {
            warnings: false,
            drop_console: true, // 传true就是干掉所有的console.*这些函数的调用.
            drop_debugger: true, // 干掉那些debugger;
            pure_funcs: ['console.log'] // 如果你要干掉特定的函数比如console.info ，又想删掉后保留其参数中的副作用，那用pure_funcs来处理
          }
        }
      }))
  },
  configureWebpack: config => {
    const plugins = [
      new NodePolyfillPlugin()
    ]
    if (process.env.NODE_ENV === 'production') {
      const arr = [
        new webpack.ProvidePlugin({
          $: 'jquery',
          jQuery: 'jquery',
          'windows.jQuery': 'jquery'
        }),
        //   new webpack.optimize.ModuleConcatenationPlugin()
        // 配置compression-webpack-plugin压缩
        new CompressionPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        }),
        new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 5 }),
        new webpack.optimize.MinChunkSizePlugin({ minChunkSize: 10000 })
      ]
      plugins.push(...arr)
    }
    return {
      resolve: {
        alias: {
          '@': resolve('src'),
          '_c': resolve('src/components')
        }
      },
      // 关闭 webpack 的性能提示
      performance: {
        hints: false
      },
      module: {
        rules: [
          {
            test: /\.js$/,
            use: ['thread-loader']
          }
        ]
      },
      plugins: plugins
    }
  }
}
